﻿var lsInfo = {
    current: 1,
    pageSize: allPageSize,
    data: [],
    totalRow: 0,
    totalPage: 0,
    oneDate: {},
    editState: false,
    state: 0
};

// document.getElementById('nav').innerHTML = template('navs', navinfo1);

$('#nav li').eq(0).addClass('active');

var getList = function (obj) {
    $.ajax({
        cache: false,
        async: false,
        url: '/api/CommonApi?API=PDGetProjects',
        type: 'get',
        data: {
            ProjectName: searchObj.ProjectName,
            StartDate: searchObj.StartDate,
            EndDate: searchObj.EndDate,
            sorts: ' StartDate DESC',
            pagesize: obj.size,
            page: obj.index
        },
        dataType: 'json',
        success: function (data) {
            // console.log(data)
            if (data.Success) {
                obj.callback(data)
            }
        }
    });
};

getList({
    size: lsInfo.pageSize,
    index: lsInfo.current,
    callback: function (data) {
        // console.log(JSON.stringify(data));
        var newData = data.ApiParamObj[0];
        for (var key in newData) {
            newData[key].StartDate = new Date(newData[key].StartDate).pattern('yyyy/MM/dd');
            newData[key].EndDate = new Date(newData[key].EndDate).pattern('yyyy/MM/dd');
            if (newData[key]['State'] === '????') {
                newData[key]['State'] = '';
            }
        }
        // 列表数据
        document.getElementById('listTable').innerHTML = template('l1List', newData);
        // 分页
        var pagesInfo = data.ApiParamObj[1];
        lsInfo.data = newData;
        lsInfo.totalRow = pagesInfo[0].totalRow;
        lsInfo.totalPage = pagesInfo[0].totalPage;

        $('#totalRow').html(lsInfo.totalRow);
        $('#totalPage').html(lsInfo.totalPage);
        pagesSet(lsInfo);
        setPageSize(lsInfo);
        loading.hide();
    }
});

// 点击分页请求事件
var showPage = function (o) {
    // console.log(o)
    // console.log(1)
    getList({
        size: o.pageSize,
        index: o.current,
        callback: function (data) {
            // console.log(JSON.stringify(data));
            var newData = data.ApiParamObj[0];
            for (var key in newData) {
                newData[key].StartDate = new Date(newData[key].StartDate).pattern('yyyy/MM/dd');
                newData[key].EndDate = new Date(newData[key].EndDate).pattern('yyyy/MM/dd');
                if (newData[key]['State'] === '????') {
                    newData[key]['State'] = '';
                }
            }
            // 列表数据
            document.getElementById('listTable').innerHTML = template('l1List', newData);
            // 分页
            var pagesInfo = data.ApiParamObj[1];
            o.data = newData;
            o.totalRow = pagesInfo[0].totalRow;
            o.totalPage = pagesInfo[0].totalPage;

            $('#totalRow').html(o.totalRow);

            pagesSet(o);
            loading.hide();
        }
    });
};

// 审批
$('#table').on('click', '.submitProject', function () {
    var _this = $(this);
    var index = _this.parent().parent().index();
    lsInfo.oneDate = lsInfo.data[index];
    $('#approve').modal('show');
});

// 通过
$('#pass').on('click', function () {
    var _this = $(this);
    // console.log(JSON.stringify(lsInfo.oneDate))
    _this.attr('disabled', 'disabled');
    $.ajax({
        url: '/api/CommonApi?API=UpdateProjectState',
        type: 'post',
        data: {
            ApiParamObj: JSON.stringify({
                ID: lsInfo.oneDate.ID,
                'State': 'PASS'
            })
        },
        dataType: 'json',
        success: function (data) {
            if (data.Success) {
                console.log('审批通过');
                _this.removeAttr('disabled');
                // _this.html('已审批').attr('disabled', 'disabled');
                showPage(lsInfo);
                $('#approve').modal('hide');
            }
        }
    });

});

// 不通过
$('#noPass').on('click', function () {
    var _this = $(this);
    // console.log(JSON.stringify(lsInfo.oneDate))
    _this.attr('disabled', 'disabled');
    $.ajax({
        url: '/api/CommonApi?API=UpdateProjectState',
        type: 'post',
        data: {
            ApiParamObj: JSON.stringify({
                ID: lsInfo.oneDate.ID,
                'State': 'NO_PASS'
            })
        },
        dataType: 'json',
        success: function (data) {
            if (data.Success) {
                console.log('审批通过');
                _this.removeAttr('disabled');
                // _this.html('已审批').attr('disabled', 'disabled');
                showPage(lsInfo);
                $('#approve').modal('hide');
            }
        }
    });
});